<!-- 商家销量的横向柱状图 -->
<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chaetInstance: null,
      allData: [{
    "name": "商家1",
    "value": 99
}, {
    "name": "商家2",
    "value": 102
}, {
    "name": "商家3",
    "value": 83
}, {
    "name": "商家4",
    "value": 49
}, {
    "name": "商家5",
    "value": 200
}, {
    "name": "商家6",
    "value": 152
}, {
    "name": "商家7",
    "value": 76
}, {
    "name": "商家8",
    "value": 23
}, {
    "name": "商家9",
    "value": 87
}, {
    "name": "商家10",
    "value": 223
}, {
    "name": "商家11",
    "value": 145
}, {
    "name": "商家12",
    "value": 187
},{
    "name": "商家13",
    "value": 127
},{
    "name": "商家14",
    "value": 57
},{
    "name": "商家15",
    "value": 99
}], //  服务器返回数据
      currentPage: 1, //当前显示的页数
      totalPage: 0, //一共多少页
      timerId: null, //定时器的标识
      titleFontSize: 0
    };
  },

  mounted() {
    
    this.initChart();
    this.getData();
    window.addEventListener('resize', this.screenAdapter)
    // 在页面加载完成的时候, 主动进行屏幕的适配
    this.screenAdapter()
  },
  destroyed() {
    clearInterval(this.timerId);
    // 在组件销毁的时候, 需要将监听器取消掉
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    //初始化echartInstance对象
    initChart() {
      this.chaetInstance = echarts.init(this.$refs.seller_ref, );
      //对图表对象进行鼠标事件的监听
      // this.chartInstance.on('mouseover',()=>{
      //     clearInterval(this.timerId)
      // })
      // this.chartInstance.on('mouseout',()=>{
      //     this.startInterval()
      // })
    },
    //获取服务器的数据
    async getData() {
      //后端接口/api/seller
      // const { data: ret } = await this.$axios.get("seller");
      // this.allData = ret;
      //对数据排序
      this.allData.sort((a, b) => {
        return a.value - b.value; //从小到大
      });
      //没5个元素显示一页
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      this.updateChart();
      //启动定时器
      this.startInterval();
    },
    //更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerNames = showData.map((item) => {
        return item.name;
      });
      const sellerValue = showData.map((item) => {
        return item.value;
      });
      const option = {
        backgroundColor:'#ffffff',
        title: {
          text: "▎商家销售表",
          padding:[20,10,5,20],
          textStyle: {
            fontSize: 12,
          },
          left: 0,
          top:-4,
        },
        grid: {
          top: "18%",
          left:"4%",
          right:"4%",
          bottom:"6%",
          containLabel:true //距离包含坐标轴的位置
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
          data: sellerNames,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              // width:66,
              coler: "#2D3443",
            },
          },
        },
        series: [
          {
            type: "bar",
            data: sellerValue,
            barWidth: 26,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: 'white'
              }
            },
            itemStyle: {
              barBorderRadius: [0, 13, 13, 0],
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                // 百分之0状态之下的颜色值
                {
                  offset: 0,
                  color: "#5052EE",
                },
                // 百分之100状态之下的颜色值
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chaetInstance.setOption(option);
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    // 当浏览器的大小发生变化的时候, 会调用的方法, 来完成屏幕的适配
    screenAdapter () {
      console.log(this.$refs.seller_ref.offsetWidth)
      const titleFontSize = this.$refs.seller_ref.offsetWidth / 100 * 3
      // 和分辨率大小相关的配置项
      const adapterOption = {
         
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      // 手动的调用图表对象的resize 才能产生效果
      this.chartInstance.resize()
    }
  },
};
</script>

<style>
</style>